package net.asfun.jvalog.misc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.logging.Level;

import static net.asfun.jvalog.misc.Log.logger;
import net.asfun.jvalog.except.InteractException;

public class Formater {

	public static String date2string(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			sdf.setTimeZone(TimeZone.getTimeZone(Setting.getTimezone()));
			return sdf.format(date);
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage(), e.getCause());
		}
		return date.toString();
	}
	
	public static Date string2date(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try { 
			sdf.setTimeZone(TimeZone.getTimeZone(Setting.getTimezone()));
			return sdf.parse(date);
		} catch (Exception e) {
			logger.log(Level.WARNING, e.getMessage(), e.getCause());
			throw new InteractException(date + " doesn't match " + format);
		}
	}

	
	public static void main(String[] args) {
		System.out.println(string2date("2009-1-00","yyyy-MM-dd"));
		System.out.println(string2date("2009-1-1","yyyy-MM-dd"));
		System.out.println(string2date("2009-1-31","yyyy-MM-dd"));
		System.out.println(string2date("2009-1-32","yyyy-MM-dd"));
		System.out.println(string2date("2009-0-01","yyyy-MM-dd"));
		System.out.println(string2date("2009-0-0","yyyy-MM-dd"));
		System.out.println(string2date("2009-1-00","yyyy-MM-dd"));
		System.out.println(string2date("2009-1-28","yyyy-MM-dd"));
		System.out.println(string2date("2009-12-1","yyyy-MM-dd"));
		System.out.println(string2date("2009-12-31","yyyy-MM-dd"));
		System.out.println(string2date("2009-13-0","yyyy-MM-dd"));
	}
}
